using System;
using System.Collections.Generic;
using System.Linq;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.IO;
using MonoTouch.SQLite;
using bjeschool_csharp.Model;

namespace bjeschool
{
	// The UIApplicationDelegate for the application. This class is responsible for launching the 
	// User Interface of the application, as well as listening (and optionally responding) to 
	// application events from iOS.
	[Register ("AppDelegate")]
	public partial class AppDelegate : UIApplicationDelegate
	{
		static public MG.MGSplitViewController split;
		static public SQLiteConnection sqlitedb;
		static public AppDelegate app;
		static public M_User loginuser;
		// class-level declarations
		private LoginController loginctrl;

		public override UIWindow Window {
			get;
			set;
		}

//		[Connect("tabControlers")]
//		public UITabBarController tabControler { get {
//				return ((UITabBarController) (this.GetNativeField("tabControlers")));
//			}
//			set {
//				this.SetNativeField("tabControlers", value);
//			} }

		public override void FinishedLaunching (UIApplication application)
		{
			app = this;
            InitDB();

			Window = new UIWindow(UIScreen.MainScreen.Bounds);
			loginctrl = new LoginController();
			//VideoController video = new VideoController();
			Window.RootViewController = loginctrl;
			Window.MakeKeyAndVisible();
		}

        private static void InitDB()
        {
            string docsDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string path = Path.Combine(docsDir, "bjeschool.sqlite");
            sqlitedb = new SQLiteConnection(path);
            Random ran = new Random();

            // create our SQLite table...
            sqlitedb.CreateTable<M_Class>();
            sqlitedb.CreateTable<M_ClassNote>();
            sqlitedb.CreateTable<M_Course>();
            sqlitedb.CreateTable<M_Group>();
            sqlitedb.CreateTable<M_HomeWork>();
            sqlitedb.CreateTable<M_HomeWorkCommit>();
            sqlitedb.CreateTable<M_Message>();
            sqlitedb.CreateTable<M_Parent>();
            sqlitedb.CreateTable<M_Student>();
			sqlitedb.CreateTable<M_ClassNoteItem>();
			sqlitedb.CreateTable<M_ClassStudentMap>();
			sqlitedb.CreateTable<M_StudentCourseApply>();
			sqlitedb.CreateTable<M_Video>();

			sqlitedb.DeleteAll<M_HomeWork>();
			string[] tmp = new string[] { "作文:  当“导游”，把浏览的顺序写清楚，写出旅游的心情。", "一双拖鞋8元，一双袜子4元。小明拿了20元钱买一双拖鞋和一双袜子，应找回多少元？" };
			if (sqlitedb.Table<M_HomeWork> ().Count () == 0) {
				M_HomeWork item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_HomeWork ();
					item.Title = tmp[i];
					item.StartTime = DateTime.Now;
					item.Deadline = DateTime.Now.AddDays(1);
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_HomeWorkCommit>();
            tmp = new string[] { "张昊宽", "岳一畅", "杨浩斌" };
			if (sqlitedb.Table<M_HomeWorkCommit> ().Count () == 0) {
				M_HomeWorkCommit item;
				
				for (int i = 0; i < 3; i++) {
					item = new M_HomeWorkCommit ();
                    item.HomeWorkRequest = "当“导游”，把浏览的顺序写清楚，写出旅游的心情。";
                    item.CommitStudent = tmp[i];
					item.CommitTime = DateTime.Now.AddMinutes(ran.Next(20,100));
					item.CommitTitle = "参观西北师大图书馆";
                    item.CommitContent = "今天，我带你去参观西北师大图书馆。 \r\n登上石阶，一抬头就望见了“图书馆”三个斗大的金字，给人一种庄严的感觉。走进大门，是个宽敞的大厅，大厅中央有一座塑像。跨进古籍室大门，迎面就看到对面墙上的敦煌壁画，阅览室的中央有几张桌子，桌子周围放着一排书架，上面摆着《四库全书》、《大藏经》等古书籍。听管理爷爷说，这里共收藏了4万多册古籍书。 \r\n师大图书馆，多像一座蕴藏着丰富知识的宝库啊!";

                    item.Result = "不错，能把浏览的顺序写清楚，写出旅游的心情。";
					item.Grade = "优";
                    item.Sheet = ran.Next(80, 100);
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_ClassNote>();
            tmp = new string[] { "二年级语文（上）", "二年级数学（上）" };
			if (sqlitedb.Table<M_ClassNote> ().Count () == 0) {
				M_ClassNote item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_ClassNote ();
					item.ClassName = "二年级（1）班";
					item.CourseName = tmp[i];
					item.CourseTime = DateTime.Now;
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_ClassNoteItem>();
			if (sqlitedb.Table<M_ClassNoteItem> ().Count () == 0) {
				M_ClassNoteItem item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_ClassNoteItem ();
					item.Name = i%2==0?"迟到":"早退";
					item.Type = NoteItemType.Sub;
					item.Sheet = -3;
					item.CreateTime = DateTime.Now;
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_Message>();
            tmp = new string[] { "我儿子最近的表现怎样？", "女儿的感冒还没好，今天请假！" };
            string[] tmp2 = new string[] { "张振宇(张昊宽的爸爸）", "张晓(郑允荷的妈妈）" };
			if (sqlitedb.Table<M_Message> ().Count () == 0) {
				M_Message item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_Message ();
					item.Content = tmp[i];
					item.Type = MessageSource.M_Sys;
					item.LeaveUser = tmp2[i];
					item.CreateTime = DateTime.Now;
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_Parent>();
			if (sqlitedb.Table<M_Parent> ().Count () == 0) {
				M_Parent item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_Parent ();
                    item.Name = i % 2 == 0 ? "张振宇" : "张晓";
                    item.Student = i % 2 == 0 ? "张昊宽" : "郑允荷";
                    item.Tel = i % 2 == 0 ? "13501270769" : "1369333089";
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_Course>();
            tmp = new string[] { "二年级语文（上）", "二年级数学（上）" };
			if (sqlitedb.Table<M_Course> ().Count () == 0) {
				M_Course item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_Course ();
					item.Name = tmp[i];
					item.State = "正常";
					item.CreateTime = DateTime.Now;
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_Class>();
			if (sqlitedb.Table<M_Class> ().Count () == 0) {
				M_Class item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_Class ();
					item.Name = string.Format("二年级({0})班", i.ToString());
					item.State = "正常";
					item.CourseName = "二年级语文（上）";
					item.CreateTime = DateTime.Now;
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_Student>();
            tmp = new string[] { "张振宇", "张昊宽" };
			if (sqlitedb.Table<M_Student> ().Count () == 0) {
				M_Student item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_Student ();
					item.Name = tmp[i];
					item.SchoolClass = "二年级(1)班";
                    item.School = "北京市东城区曙光小学";
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_Student>();
			if (sqlitedb.Table<M_StudentCourseApply> ().Count () == 0) {
				M_StudentCourseApply item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_StudentCourseApply ();
					item.CourseName = "二年级语文（上）";
					item.StudentName = tmp[i];
					item.CreateTime = DateTime.Now;
					
					sqlitedb.Insert (item);
				}
			}
			//sqlitedb.DeleteAll<M_Video>();
			tmp = new string[]{"张荣老师", "陈晓老师"};
			if (sqlitedb.Table<M_Video> ().Count () == 0) {
				M_Video item;
				
				for (int i = 0; i < 2; i++) {
					item = new M_Video ();
					item.Name = "二年级英语（上）语音教学直播";
					item.TeacherName = tmp[i];
					item.CreateTime = DateTime.Now;
					item.Type = i%2==0?VideoType.LiveVideo:VideoType.Open;
					
					sqlitedb.Insert (item);
				}
			}
		}
	}
}

